/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package siteanaliz;
import java.awt.Cursor;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.lang.Thread;
import java.io.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;


/**
 *
 * @author Никита
 */
public class Top3Runnable implements Runnable  {
    JLabel c;
    
    private String key;
    private String SE;
    private int position;
    private String langa;
    private JPanel tt;
    private GridBagLayout layout ;
     private JTable tablet;
   
    public Top3Runnable(String lang, JPanel ff,GridBagLayout layout1, JTable t, String Engine, int poses, String tag )
    {
         c=new JLabel("Stand by. Loading...");
         layout=layout1;
        GridBagConstraints propert;
        propert = new GridBagConstraints();
         propert.gridx = 2;
         propert.gridy = 3;
         propert.insets = new Insets(5,10, 0, 0);
         layout.setConstraints(c, propert);
         c.setVisible(true);
         tt=ff;
         tt.add(c);
         SE=Engine;
         position=poses;
         langa=lang;
         key=tag;
         tablet=t;
         
    }
     
    public void run()
    {
            try
            {
               
                Searcher ss=new Searcher(this.SE,this.langa);
                ParseEngine seodigit=new ParseEngine(SE);
                String p1=seodigit.Get3Concurents(position,ss, key);
                 System.out.println("Конкуренты "+p1);
                int Conc=ConCount(p1);
                c.setVisible(false);
                if(Conc==-1 || Conc==-2)
                     addPosition("nothing","nothing",tablet );
                else
                {
                    for(int i=1; i<=Conc; i++)
                    {
                            addPosition(SiteD(GetCUrl(p1,i )),GetCUrl(p1,i ),tablet );
                    }  
                }
                
            }
             catch(Exception e)
            {
                System.out.println("inthread exeption");
            }
            finally
            {
          
            }
            
    }
    
     private int ConCount(String ss)
    {
        if(ss.equals("unknown")) return -1;
        if(ss.equals("Captcha")) return -2;
        int firstI=0;
        int secondI=0;
        int count=0;
        while(firstI!=-1)
        {
            firstI=ss.indexOf("@",secondI);
            secondI=ss.indexOf("@",firstI+1)+1;
            // System.out.println(ss.substring(firstI+1, secondI-1));
            count++;
        }
        return count-1;
       
    }
     
    private String countnum(String ss,int count)
    {
        int firstI=0;
        int secondI=0;
        for(int i=0; i<count; i++)
        {
            firstI=ss.indexOf("@",secondI)+1;
            secondI=ss.indexOf("@",firstI)+1;
            //System.out.println(ss.substring(firstI, secondI-1));
        }
        return ss.substring(firstI, secondI-1);
    }
    
    private String SiteD(String ss)
    {
        int firstI=0;
        int secondI=0;
        
            firstI=ss.indexOf("http://");
            if(firstI!=-1) firstI+=7;
            else 
            {
                firstI=ss.indexOf("https://");
                firstI+=8;
            }
            
            secondI=ss.indexOf("/",firstI);
            //System.out.println(ss.substring(firstI, secondI-1));
        
        return ss.substring(firstI, secondI);
    }
    
    private String GetCUrl(String ss,int count )
    {
        int firstI=0;
        int secondI=0;
        for(int i=0; i<count; i++)
        {
            firstI=ss.indexOf("$U",secondI)+2;
            secondI=ss.indexOf("$U",firstI)+2;
            //System.out.println(ss.substring(firstI, secondI-1));
        }
        return ss.substring(firstI, secondI-2);
    }
    
     private void addPosition(String Site, String url, JTable table1 ) {
 // Добавить URL в таблицу совпадений.
        
            DefaultTableModel model =
            (DefaultTableModel) table1.getModel();
            model.addRow(new Object[]{Site,url});
        
        }
}
